/**
 * <b>项目名：</b>ht-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>PersonInfoServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2019年8月8日 上午11:26:50<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flea.hclv.model.dao.PersonInfoMapper;
import com.flea.hclv.model.entity.PersonInfo;
import com.flea.hclv.service.PersonInfoService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.utils.FleaStringUtils;

/**
 * @description TODO
 * @createTime 2019年8月8日 上午11:26:50
 * @modifyTime 
 * @author myflea@163.com
 * @version 1.0
 */
@Service
public class PersonInfoServiceImpl implements PersonInfoService {
	@Autowired
	private PersonInfoMapper personInfoMapper;

	@Override
	public ServerResponse<String> saveOrUpdate(String userId,
			OrgDept currentUserDept, PersonInfo personInfo) {

        if (personInfo==null){
            return ServerResponse.createByErrorMessage("人员信息为空");
        }
        String id = personInfo.getId();
        int result;
        if (id == null) {
        	
        	//获取身份证号码，然后判断系统中该身份证用户的人员是否已经存在
        	
        	
        	PersonInfo resultCheck  =	personInfoMapper.findByCardSn(personInfo.getCardSn());
        	if(resultCheck!=null) {
        		 return ServerResponse.createByErrorMessage("该人员已经存在");
        	}
        	
            id = FleaStringUtils.getGenerateUUID();
            personInfo.setId(id);
           
            personInfo.setCreateUser(userId);
            personInfo.setModifyUser(userId);
            personInfo.setOperType("0");
            result = personInfoMapper.insertSelective(personInfo);
            if (result<0){
                return ServerResponse.createByErrorMessage("人员添加失败");
            }
        }else {
        	//需要判断哪些信息可以修改，哪些信息不可以修改
        	personInfo.setOperType("0");
        	personInfo.setModifyUser(userId);
        	personInfo.setValid("Y");
            result = personInfoMapper.updateByPrimaryKeySelective(personInfo);
            if (result<0){
                return ServerResponse.createByErrorMessage("人员更新失败");
            }
        }
        return ServerResponse.createBySuccessMessage("操作成功");
	}

	@Override
	public ServerResponse<FleaPage<PersonInfo>> selectAll(int curPage, int pageSize, String projectCode, String name,
			String personNo,String sex, String startTime, String endTime) {
		  RowBounds rowBounds = new RowBounds(curPage,pageSize);
	        List<PersonInfo> list = personInfoMapper.selectAll(rowBounds,projectCode,name,personNo,sex,startTime,endTime);
	        
	        
	        
	        
	        return ServerResponse.createBySuccess(new FleaPage<>(list));
	}

	@Override
	public ServerResponse<String> deleteByIds(String userId, List<String> idList) {
		 int result = personInfoMapper.deleteByIds(idList,userId);
	        if (result<0){
	            return ServerResponse.createByErrorMessage("删除失败");
	        }
	        return ServerResponse.createBySuccessMessage("删除成功");
	}

}
